package com.tencent;

import com.cnb.util.SnUtil;
import com.tencent.common.Configure;
import com.tencent.common.Signature;
import com.tencent.common.Util;
import com.tencent.protocol.downloadbill_protocol.DownloadBillReqData;
import com.tencent.protocol.pay_protocol.ScanPayReqData;
import com.tencent.protocol.pay_query_protocol.ScanPayQueryReqData;
import com.tencent.protocol.refund_protocol.RefundReqData;
import com.tencent.protocol.refund_query_protocol.RefundQueryReqData;
import com.tencent.protocol.reverse_protocol.ReverseReqData;
import com.tencent.protocol.reverse_protocol.ReverseResData;
import com.tencent.service.DownloadBillService;
import com.tencent.service.RefundQueryService;
import com.tencent.service.RefundService;
import com.tencent.service.ReverseService;
import com.tencent.service.ScanPayQueryService;
import com.tencent.service.ScanPayService;

import java.util.Date;
import java.util.UUID;

public class Main {
	
	public static void testQuery(){
		try {
			ScanPayQueryService scanPayQueryService=new ScanPayQueryService();
			ScanPayQueryReqData scanPayQueryReqData=new ScanPayQueryReqData("4005302001201701247398042093", "20170124020820597");
		    String xmlString=    scanPayQueryService.request(scanPayQueryReqData);
		    System.out.print(xmlString);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static ScanPayReqData packScanPayReqData(){
		String authCode="130238362049626756";
		String body="1222";
		String attach="支付测试";
		String outTradeNo=SnUtil.getSN();
		int totalFee=1;
		String deviceInfo="银行设备";
		String spBillCreateIP="127.0.0.1";
		
		String timeStart="20170124091010";
		String timeExpire="20170124221010";
		String goodsTag="测试";
		ScanPayReqData scanPayReqData=new ScanPayReqData(authCode, body, 
				attach, 
				outTradeNo, totalFee, deviceInfo, spBillCreateIP, timeStart, timeExpire, goodsTag);
		
		return scanPayReqData;
	}
	/**
	 * 撤销接口，即冲正接口
	 */
	public static void testReverse(){
		try {
			ReverseService reverseService=new ReverseService();
			ReverseReqData reverseReqData=new ReverseReqData("4005302001201701247398042093", "20170124020820597");
			reverseService.request(reverseReqData);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void testPay(){
		try {
			ScanPayService payService=new ScanPayService();
			
			ScanPayReqData scanPayReqData=packScanPayReqData();
			
			 String jsonString= payService.request(scanPayReqData);
			 
			 System.out.print(jsonString);
			 
		} catch (IllegalAccessException | InstantiationException
				| ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

    public static void main(String[] args) {

        try {

        	
            //--------------------------------------------------------------------
            //温馨提示，第一次使用该SDK时请到com.tencent.common.Configure类里面进行配置
            //--------------------------------------------------------------------



            //--------------------------------------------------------------------
            //PART One:基础组件测试
            //--------------------------------------------------------------------

            //1）https请求可用性测试
       //     HTTPSPostRquestWithCert.test();

        	
            //2）测试项目用到的XStream组件，本项目利用这个组件将Java对象转换成XML数据Post给API
            //XStreamTest.test();


            //--------------------------------------------------------------------
            //PART Two:基础服务测试
            //--------------------------------------------------------------------

            //1）测试被扫支付API
        	//   testPay();
             //testPay();
        	
            //2）测试被扫订单查询API
            //PayQueryServiceTest.test();
        	//testQuery()
        	
            //3）测试撤销API
            //温馨提示，测试支付API成功扣到钱之后，可以通过调用PayQueryServiceTest.test()，将支付成功返回的transaction_id和out_trade_no数据贴进去，完成撤销工作，把钱退回来 ^_^v
            //ReverseServiceTest.test();
            //  testReverse(); 冲正接口
            //4）测试退款申请API
        	//testRefund();
             
            //5）测试退款查询API
            // testRefundQuery();
            //6）测试对账单API
            //DownloadBillServiceTest.test();
        	 testDownloadBill();

            //本地通过xml进行API数据模拟的时候，先按需手动修改xml各个节点的值，然后通过以下方法对这个新的xml数据进行签名得到一串合法的签名，最后把这串签名放到这个xml里面的sign字段里，这样进行模拟的时候就可以通过签名验证了
           // Util.log(Signature.getSignFromResponseString(Util.getLocalXMLString("/test/com/tencent/business/refundqueryserviceresponsedata/refundquerysuccess2.xml")));

            //Util.log(new Date().getTime());
            //Util.log(System.currentTimeMillis());

        } catch (Exception e){
            Util.log(e.getMessage());
        }

    }

	private static void testDownloadBill() {
		// TODO Auto-generated method stub
		DownloadBillService downloadBillService;
		try {
			downloadBillService = new DownloadBillService();
			DownloadBillReqData downloadBillReqData=new DownloadBillReqData("银行设备", "20161203", "SUCCESS");
	    	String reponse= downloadBillService.request(downloadBillReqData);
	    	System.out.print(reponse);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}

	public static void testRefundQuery() {
		// TODO Auto-generated method stub
		
		try {
			RefundQueryService refundQueryService=new RefundQueryService();
		   RefundQueryReqData refundQueryReqData=new RefundQueryReqData(
			    	"4005302001201701247407663806", "20170124030645429", "银行设备", "20170124030746276", "2005302001201701240774663027");
		 String reponseString=	refundQueryService.request(refundQueryReqData);
		 System.out.print(reponseString);
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public static void testRefund() {
		// TODO Auto-generated method stub
		try {
			String outRefundNo=SnUtil.getSN();
			RefundService refundService=new RefundService();
			RefundReqData refundReqData=new RefundReqData("4005302001201701247407663806", "20170124030645429",
					"银行设备", outRefundNo, 1, 1, Configure.getMchid(), "CNY");
		    String result=	refundService.request(refundReqData);
		    System.out.println(result);
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
